March 12, 2015
ggplot(mtcars, aes(x=wt, y=mpg))+ geom_point()
mtcars %>% ggvis(x= ~wt, y= ~mpg) %>% layer_points()
mtcars %>% ggvis(x= ~wt, y= ~mpg) %>% layer_points() %>% layer_smooths()
| ggplot2 | ggvis |
|---|---|
| geom | layer (names end with "s") |
| stat | compute |
| aes() | props() |
%>% rather than +x = ~wt instead of x = wt (will come back to this)ggvis() without any layers is akin to qplot<data> %>%
ggvis(~<x property>,~<y property>,
fill = ~<fill property>, ...) %>%
layer_<marks>()`
<marks> can be arcs, bars, densities, freqpolys, histograms, images, lines, paths, model_predictions, points, rects, ribbons, smooths, text
faithful %>%
ggvis(~waiting, ~eruptions, fill := "red") %>%
layer_points() %>%
add_axis("y", title = "Duration of eruption (m)",
values = c(2, 3, 4, 5), subdivide = 9) %>%
add_axis("x", title = "Time since previous eruption (m)")
= and :== maps a property to a data value or a set of data values:= sets a property to a specific valuepressure %>% ggvis(~temperature, ~pressure, fill = "red") %>% layer_points()
= and :== maps a property to a data value or a set of data values:= sets a property to a specific valuepressure %>% ggvis(~temperature, ~pressure, fill := "red") %>% layer_points()
= and :=See http://ggvis.rstudio.com/properties-scales.html for more details.
ggvis code accepts three types of objects:
~ at the start of the string will be treated as a variable name, and will be searched in the current data frameshade <- 'red'
mtcars %>% ggvis(~disp, ~mpg) %>%
layer_points(fill= ~factor(cyl), size :=300, stroke := shade,
strokeWidth :=6) %>% add_legend('fill',title='Cylinders')
mtcars %>% group_by(am) %>%
ggvis(~mpg, ~hp) %>% layer_smooths(stroke = ~factor(am)) %>%
layer_points(fill = ~factor(am))
mtcars %>% group_by(am) %>%
ggvis(~mpg, ~hp) %>% layer_smooths(stroke = ~factor(am)) %>%
layer_points(fill = ~factor(am)) %>%
scale_nominal('fill', range=c("green","orange")) %>%
scale_nominal('stroke', range=c('green','orange'))
base <- mtcars %>% ggvis(~mpg, ~cyl) %>% layer_points() base %>% group_by(cyl) %>% summarise(mpg = mean(mpg)) %>% layer_points(fill := "red", size := 100)
mtcars %>% ggvis(~wt, ~mpg) %>% layer_points() %>% layer_smooths(se=T, span=input_slider(0.1,0.9,value=0.75,step=0.05,label='span'))
## Warning: Can't output dynamic/interactive ggvis plots in a knitr document. ## Generating a static (non-dynamic, non-interactive) version of the plot.
mtcars %>% mutate(name=row.names(mtcars)) %>% ggvis(~wt, ~mpg, key := ~name) %>% layer_points() %>% add_tooltip(function(df) df$name)
mtcars %>% ggvis(~wt, ~mpg) %>% layer_points() %>% layer_model_predictions(model = "lm", stroke:='black') %>% layer_model_predictions(model = "loess", stroke := "red")